<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="applicable-device" content="pc">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link rel="icon" href="">
    <link rel="stylesheet" href="/static/index/css/reset.css" />
    <link rel="stylesheet" href="/static/index/lib/bootstrap.min.css" />
    <!--<link rel="stylesheet" href="/static/index/lib/bootstrap.css">-->
    <link rel="stylesheet" href="/static/index/css/common.css">
    <link rel="stylesheet" href="/static/index/css/home.css">
    <link rel="stylesheet" href="/static/index/css/users.css">
    <script src="/static/index/lib/jquery-2.1.4.js"></script>
    <!--<script src="/static/index/lib/jquery-2.1.4.js"></script>-->
    <script src="/static/index/lib/bootstrap.min.js"></script>
    <script type="text/javascript" src="/static/index/lib/template.js"></script>
    <script src="/static/index/layer/layer.js"></script>
    <script>
            var _hmt = _hmt || [];
            (function() {
              var hm = document.createElement("script");
              hm.src = "https://hm.baidu.com/hm.js?8b340ebdd740974de9d621b14ab09b43";
              var s = document.getElementsByTagName("script")[0]; 
              s.parentNode.insertBefore(hm, s);
            })();
        </script>
</head>

<body>
    {include file="public/push"}
    {include file="public/usersHeader"}
    <div class="widthFullScreen">
        <div class="wdSafe user_body clearfix">
            {include file="public/usersLeftNav"}
            <!-- left over -->
            <div class="pull-right right_wrap">
                <div class="nav_common border-top">
                    <ol class="breadcrumb pull-left">
                        <li>
                            <a href="#">用户中心</a>
                        </li>
                        <li>
                            <a href="#">我的资料</a>
                        </li>
                        <li class="active">公司资料</li>
                    </ol>
                </div>
                <div class="comUser_wrap">
                    <div class="form-horizontal">
                        <div class="form-group">
                            <label for="one" class="col-sm-2 control-label">
                                <span class="colorRed">*</span>&nbsp;&nbsp;公司介绍
                            </label>
                            <div class="col-sm-10">
                                <textarea class="form-control textarea_style" placeholder="请输入公司的详细信息" name="" rows="" cols="" id="desc"></textarea>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="two" class="col-sm-2 control-label">
                                <span class="colorRed">*</span>&nbsp;&nbsp;公司优势
                            </label>
                            <div class="col-sm-10">
                                <textarea class="form-control textarea_style" style="height: 150px;" placeholder="请输入公司优势" name="" rows="" cols="" id="advantage"></textarea>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="two" class="col-sm-2 control-label">
                                <span class="colorRed">*</span>&nbsp;&nbsp;资质类别
                            </label>
                            <div class="col-sm-10">
                                <div id="chooseOne"></div>
                                <script id="chooseOneScript" type="text/html">
                                    <select class="selectStyle pull-left" name="" id="chooseOneSelect" style="margin-right: 20px;">
                                        <option value="">选择资质类别</option>
                                        {{each data item i}}
                                        <option value="{{item.id}}">{{item.category_name}}</option>
                                        {{/each}}
                                    </select>
                                </script>
                                <div id="chooseTwo"></div>
                                <script id="chooseTwoScript" type="text/html">
                                    <select class="selectStyle pull-left" name="" id="chooseTwoSelect">
                                        <option value="">选择资质专业</option>
                                        {{each subcategoy itemTwo i}}
                                        <option value="{{itemTwo.id}}">{{itemTwo.category_name}}</option>
                                        {{/each}}
                                    </select>
                                </script>
                                <div class="addBtn pull-left"></div>
                            </div>
                            <div class="col-sm-10 col-md-offset-2">
                                <div class="row" id="catGeryChoosed"></div>
                                <script id="catGeryChoosedScript" type="text/html">
                                    {{each subCatName item index}}
                                    <div class="col-xs-4 col-md-4 zizhi_box">
                                        <div class="zizhi_name pull-left">{{item.name}}</div>
                                        <div class="reduceBtn pull-left" onclick="reduceChoosed({{item.id}})"></div>
                                    </div>
                                    {{/each}}
                                </script>
                            </div>
                        </div>
                        <form method="post" id="logoForm" enctype="multipart/form-data">
                            <div class="form-group">
                                <label for="" class="col-sm-2 control-label">
                                    <span class="colorRed">*</span>&nbsp;&nbsp;公司LOGO
                                </label>
                                <div class="col-sm-7">
                                    <div class="imgWrap_upload">
                                        <div class="upLoadImg_style">
                                            <img id="logoPic" src="https://img.qikuai100.com/bg.png" />
                                            <input type="file" name="file" id="logoIpt" accept="image/*" />
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">
                                <span class="colorRed">*</span>&nbsp;&nbsp;公司图册
                            </label>
                            <div class="col-sm-7">
                                <div class="imgWrap_upload" id="compPicContainer">
                                    <form method="post" id="compFormOne" enctype="multipart/form-data">
                                        <div class="upLoadImg_style">
                                            <img id="comPicOne" src="https://img.qikuai100.com/bg.png" />
                                            <input type="file" name="file" id="comIptOne" accept="image/*" />
                                        </div>
                                    </form>
                                    <form method="post" id="compFormTwo" enctype="multipart/form-data">
                                        <div class="upLoadImg_style">
                                            <img id="comPicTwo" src="https://img.qikuai100.com/bg.png" />
                                            <input type="file" name="file" id="comIptTwo" accept="image/*" />
                                        </div>
                                    </form>
                                    <form method="post" id="compFormThree" enctype="multipart/form-data">
                                        <div class="upLoadImg_style">
                                            <img id="comPicThree" src="https://img.qikuai100.com/bg.png" />
                                            <input type="file" name="file" id="comIptThree" accept="image/*" />
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="two" class="col-sm-2 control-label">
                                <span class="colorRed">*</span>&nbsp;&nbsp;对接人信息
                            </label>
                            <div class="col-sm-10">
                                <input type="text" placeholder="请输入姓名" class="form-control ipt_style" name="" id="contact_name" value="" />
                                <input type="number" placeholder="请输入联系方式" style="margin-top: 15px;" class="form-control ipt_style" name="" id="contact_number" value="" />
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <div class="submitBtn" onclick="saveCompany()">保存</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- right over -->
        </div>
    </div>
    <div class="widthFullScreen heightGray"></div>
    {include file="public/footer"}
    <!-- <script type="text/javascript" src="/static/index/js/companyInfor.js"></script> -->
    <script>
    var baseUrl = 'https://api.qikuai100.com/';
    var catgeroyList = {};
    // 选择的资质等级
    var subcategoryData = {
        subcategoryData: [],
        subCatName: []
    };

    var uid = localStorage.getItem("uid");
    $('.userId').val(uid);


    var logoSrc = ''; // 公司logo 地址

    $(function() {

        initDateOne();
        $('.addBtn').click(function() {
            addCatgery();
        });

        initInfor();
        /**
         * 上传Logo
         */
        var uploadingLogo = false;
        $("#logoIpt").on("change", function() {
            if (uploadingLogo) {
                layer.msg("文件正在上传中，请稍候");
                return false;
            }
            var indexLogo = layer.load(1, { shade: false });
            $.ajax({
                url: baseUrl + "api/img/upload",
                type: 'POST',
                cache: false,
                data: new FormData($('#logoForm')[0]),
                processData: false,
                contentType: false,
                dataType: "json",
                beforeSend: function() {
                    uploadingLogo = true;
                },
                success: function(data) {
                    layer.close(indexLogo);
                    console.log(data);
                    if (data.ret == 0) {
                        $("#logoPic").attr("src", data.data.url);
                    } else {
                        layer.msg("上传logo失败");
                    }
                    uploadingLogo = false;
                }
            });
        });

        /**
         * 上传公司图片 1
         */
        var uploadingCompOne = false;
        $("#comIptOne").on("change", function() {
            if (uploadingCompOne) {
                layer.msg("文件正在上传中，请稍候");
                return false;
            }
            var indexLogo = layer.load(1, { shade: false });
            $.ajax({
                url: baseUrl + "api/img/upload",
                type: 'POST',
                cache: false,
                data: new FormData($('#compFormOne')[0]),
                processData: false,
                contentType: false,
                dataType: "json",
                beforeSend: function() {
                    uploadingCompOne = true;
                },
                success: function(data) {
                    layer.close(indexLogo);
                    console.log(data);
                    if (data.ret == 0) {
                        $("#comPicOne").attr("src", data.data.url);
                    } else {
                        layer.msg("上传公司图片失败");
                    }
                    uploadingCompOne = false;
                }
            });
        });

        /**
         * 上传公司图片 2
         */
        var uploadingCompTwo = false;
        $("#comIptTwo").on("change", function() {
            if (uploadingCompTwo) {
                layer.msg("文件正在上传中，请稍候");
                return false;
            }
            var indexLogo = layer.load(1, { shade: false });
            $.ajax({
                url: baseUrl + "api/img/upload",
                type: 'POST',
                cache: false,
                data: new FormData($('#compFormTwo')[0]),
                processData: false,
                contentType: false,
                dataType: "json",
                beforeSend: function() {
                    uploadingCompTwo = true;
                },
                success: function(data) {
                    layer.close(indexLogo);
                    if (data.ret == 0) {
                        $("#comPicTwo").attr("src", data.data.url);
                    } else {
                        layer.msg("上传公司图片失败");
                    }
                    uploadingCompTwo = false;
                }
            });
        });

        /**
         * 上传公司图片 3
         */
        var uploadingCompThree = false;
        $("#comIptThree").on("change", function() {
            if (uploadingCompThree) {
                layer.msg("文件正在上传中，请稍候");
                return false;
            }
            var indexLogo = layer.load(1, { shade: false });
            $.ajax({
                url: baseUrl + "api/img/upload",
                type: 'POST',
                cache: false,
                data: new FormData($('#compFormThree')[0]),
                processData: false,
                contentType: false,
                dataType: "json",
                beforeSend: function() {
                    uploadingCompThree = true;
                },
                success: function(data) {
                    layer.close(indexLogo);
                    if (data.ret == 0) {
                        $("#comPicThree").attr("src", data.data.url);
                    } else {
                        layer.msg("上传公司图片失败");
                    }
                    uploadingCompThree = false;
                }
            });
        });

    });

    
    /**
    * 初始化
    * 0=>'未认证', 1=>'审核中',2=>'未通过' 3 审核通过
    */
    function initInfor(){

        $.ajax({
            type: "POST",
            url: baseUrl + "api/company/info",
            data: {
                "uid": uid
            },
            success: function(result) {
                console.log(result.data);
                var dataL = result.data;
                if(result.ret == 0){
                    if(!dataL.company.advantage){
                        return;
                    }
                    $('#desc').val(dataL.company.desc);
                    $('#advantage').val(dataL.company.advantage);
                    $('#contact_name').val(dataL.company.contact_name);
                    $('#contact_number').val(dataL.company.contact_number);
                    // 图片
                    $("#logoPic").attr("src", dataL.company.logo);
                    if(dataL.company.ablum[0]){
                        $("#comPicOne").attr("src", dataL.company.ablum[0]);
                    }
                    if(dataL.company.ablum[1]){
                        $("#comPicTwo").attr("src", dataL.company.ablum[1]);
                    }
                    if(dataL.company.ablum[2]){
                        $("#comPicThree").attr("src", dataL.company.ablum[2]);
                    }
                    // 资质
                    subcategoryData.subCatName = dataL.company.categorys;
                    for(var i = 0; i < dataL.company.categorys.length; i++){
                        subcategoryData.subcategoryData.push(dataL.company.categorys[i].id);
                    }
                    var catGeryChoosed = template('catGeryChoosedScript', subcategoryData);
                    document.getElementById('catGeryChoosed').innerHTML = catGeryChoosed;
                    
                }else{
                    layer.msg(result.res_info);
                }
            }
        });
    }


    var catGeryChoosed = template('catGeryChoosedScript', subcategoryData);
    document.getElementById('catGeryChoosed').innerHTML = catGeryChoosed;

    /**
     *添加已选中的资质 *
     */
    function addCatgery() {
        var idOne = $('#chooseOneSelect').children('option:selected').val(),
            idTwo = $('#chooseTwoSelect').children('option:selected').val(), // 2级资质id
            nameOne = '',
            nameTwo = '',
            str = '';
        if (idOne == '') {
            layer.msg("请选择资质类别");
            return false;
        }
        if (idTwo == '') {
            layer.msg("请选择资质专业");
            return false;
        }

        for (var i = 0; i < catgeroyList.data.length; i++) {
            if (idOne == catgeroyList.data[i].id) {
                nameOne = catgeroyList.data[i].category_name;
                for (var j = 0; j < catgeroyList.data[i].subcategoy.length; j++) {
                    if (idTwo == catgeroyList.data[i].subcategoy[j].id) {
                        nameTwo = catgeroyList.data[i].subcategoy[j].category_name;
                    }
                }
            }
        }
        str = nameOne + '-' + nameTwo;
        // 插入选择的资质
        if (subcategoryData.subcategoryData.length == 0) {
            subcategoryData.subcategoryData.push(idTwo);
            var jsonCreat = {
                name: str,
                id: idTwo
            };
            subcategoryData.subCatName.push(jsonCreat);
        } else {
            for (var g = 0; g < subcategoryData.subcategoryData.length; g++) {
                if (subcategoryData.subcategoryData[g] == idTwo) {
                    layer.msg("请勿重复添加相同的资质");
                    return false;
                }
            }
            subcategoryData.subcategoryData.push(idTwo);
            var jsonCreat = {
                name: str,
                id: idTwo
            };
            subcategoryData.subCatName.push(jsonCreat);
        }
        var catGeryChoosed = template('catGeryChoosedScript', subcategoryData);
        document.getElementById('catGeryChoosed').innerHTML = catGeryChoosed;
        console.log(subcategoryData);
    }

    /**
     *删除已选中的资质 *
     */
    function reduceChoosed(dId) {
        for (var i = 0; i < subcategoryData.subcategoryData.length; i++) {
            if (subcategoryData.subcategoryData[i] == dId) {
                var arr = subcategoryData.subcategoryData;
                arr.splice($.inArray(dId, arr), 1)
                i--;
            }
        }
        for (var j = 0; j < subcategoryData.subCatName.length; j++) {
            if (subcategoryData.subCatName[j].id == dId) {
                var arrN = subcategoryData.subCatName;
                arrN.splice($.inArray(subcategoryData.subCatName[j], arrN), 1);
                j--;
            }
        }
        var catGeryChoosed = template('catGeryChoosedScript', subcategoryData);
        document.getElementById('catGeryChoosed').innerHTML = catGeryChoosed;
    }
    // 资质分类
    function initDateOne() {
        $.ajax({
            type: "POST",
            url: baseUrl + "api/category/list",
            data: {
                "type": 1
            },
            success: function(result) {
                if (result.ret == 0) {
                    catgeroyList = result;
                    console.log(catgeroyList);
                    var chooseOne = template('chooseOneScript', catgeroyList);
                    document.getElementById('chooseOne').innerHTML = chooseOne;
                }
            }
        });
    }

    // 选择资质专业
    var twoCatList = {
        name: "资质专业",
        subcategoy: []
    };
    $('#chooseOne').on("change", "#chooseOneSelect", function() {
        var ss = $(this).children('option:selected').val();
        if (ss) {
            for (var i = 0; i < catgeroyList.data.length; i++) {
                if (ss == catgeroyList.data[i].id) {
                    twoCatList.subcategoy = catgeroyList.data[i].subcategoy;
                }
            }
        }
        var chooseTwo = template('chooseTwoScript', twoCatList);
        document.getElementById('chooseTwo').innerHTML = chooseTwo;
    });
    // 初始化资质专业
    var chooseTwo = template('chooseTwoScript', twoCatList);
    document.getElementById('chooseTwo').innerHTML = chooseTwo;

    /**
     * 公司图片
     */
    var arrPicList = [];

    function checkCompPic() {
        var basePicUrl = 'https://img.qikuai100.com/bg.png';
        $('#compPicContainer').find("img").each(function(i, item) {
            if ($(this).attr("src") != basePicUrl) {
                arrPicList.push($(this).attr("src"));
            }
        });
    }

    /**
     * 提交数据
     */
    function saveCompany() {
        var desc = $('#desc').val();
        var advantage = $('#advantage').val();
        var contact_name = $('#contact_name').val();
        var contact_number = $('#contact_number').val();
        var myreg = /^1[34578]\d{9}$/;
        checkCompPic();
        // console.log(arrPicList);
        var basePicUrl = 'https://img.qikuai100.com/bg.png';

        if ($.trim(desc) == '') {
            layer.msg("请输入公司介绍");
            return;
        }
        if ($.trim(advantage) == '') {
            layer.msg("请输入公司优势");
            return;
        }

        if (subcategoryData.subcategoryData.length == 0) {
            layer.msg("请选择公司代办资质");
            return;
        }
        if ($("#logoPic").attr("src") == basePicUrl) {
            layer.msg("请上传公司logo");
            return;
        }
        if (arrPicList.length == 0) {
            layer.msg("请上传公司图片");
            return;
        }
        if ($.trim(contact_name) == '') {
            layer.msg("请输入对接人信息");
            return;
        }
        if ($.trim(contact_number) == '') {
            layer.msg("请输入对接人电话号码");
            return;
        }
        if (!myreg.test(contact_number)) {
            layer.msg("请输入有效的手机号码");
            return;
        }
        var subcategory_ids = {
            subcategory_ids: subcategoryData.subcategoryData
        };
        // console.log(subcategory_ids);
        var album = {
            album: arrPicList
        };

        $.ajax({
            type: "POST",
            url: baseUrl + "api/company/upload",
            data: {
                "uid": uid,
                "desc": desc,
                "advantage": advantage,
                "subcategory_ids": subcategory_ids,
                "logo": $("#logoPic").attr("src"),
                "album": album,
                "contact_name": contact_name,
                "contact_number": contact_number
            },
            success: function(result) {
                console.log(result);
                if (result.ret == 0) {
                    layer.msg("信息保存成功！");
                } else {
                    layer.msg(result.res_info);
                }
            }
        });
    }
    </script>
</body>

</html>